package com.educal.Controller;

import com.educal.Bean.Admin;
import com.educal.Repository.AdminRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by miyang on 2017/12/17.
 */
@RestController
public class Admin_AdminController {
    @Autowired
    private AdminRepository adminRepository;

    /*
    * 增加管理员*/
    @PostMapping(value = "/admins")
    public Map<String, Object> addAdmin(@RequestParam("adminId") String adminId,
                          @RequestParam("adminName") String adminName,
                          @RequestParam("adminPassword") String adminPassword){
        Admin admin = new Admin();
        Map<String, Object> result = new HashMap<String, Object>();
        if(adminId!=null&&adminName!=null&&adminPassword!=null){
            result.put("code", 200);
        }
        else
            result.put("code", 400);
        admin.setAdminid(adminId);
        admin.setAdminname(adminName);
        admin.setAdminpassword(adminPassword);
         adminRepository.save(admin);
//        if(admin.getAdminid()!=null && admin.getAdminname()!=null) {
//            result.put("code", 200);
//            //result.put("admin", admin);
//        }else{
//            result.put("code", 400);
//        }
        //System.out.print(stu.getStuid()+ stu.getStuname());
        return result;
    }

    /*
    * 查询全部管理员信息*/
    @GetMapping(value= "/admins")
    public Map<String, Object> findAll() {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Admin> adminList = adminRepository.findAll();
        if(adminList!=null&&!adminList.isEmpty()){
            result.put("code",200);
            result.put("adminList",adminList);
        }else{
            result.put("code",400);
        }
        return result;
    }

    /*
    * 用id查询*/
    @GetMapping(value = "/admins/{adminId}")
    public Map<String, Object> findByAdminId(@PathVariable("adminId") String adminId) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code",400);
        Admin admin = new Admin();
        List<Admin> list = adminRepository.findAll();
        for(int i=0;i<list.size();i++){
            String aId = list.get(i).getAdminid();
            if(aId.equals(adminId)){
                admin.setAdminid(adminId);
                admin.setAdminname(list.get(i).getAdminname());
                admin.setAdminpassword(list.get(i).getAdminpassword());
                result.put("code",200);
                result.put("admin",admin);
                break;
            }
        }
        return result;
    }
    /*
    * 用name查询*/
    @GetMapping(value = "/admins/name/{adminName}")
    public Map<String,Object> findByName(@PathVariable("adminName") String adminName) {
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",400);
        List<Admin> adminList = new ArrayList<>();
        List<Admin> list = adminRepository.findAll();
        for(int i=0;i<list.size();i++){
            String aName = list.get(i).getAdminname();
            if(aName.equals(adminName)){
                adminList.add(list.get(i));
            }
        }
        if(adminList!=null&&!adminList.isEmpty()){
            result.put("code",200);
            result.put("stuList",adminList);
        }
        // result.put("stuList",stuList);
        return result;
    }

    /*
    * 修改管理员信息*/
    @PutMapping(value = "/admins")
    public Map<String, Object> updateAdminId(@RequestParam("adminId") String adminId,
                               @RequestParam("adminName") String adminName,
                               @RequestParam("adminPassword") String adminPassword){
        Admin admin = new Admin();
        Map<String, Object> result = new HashMap<String, Object>();
        admin.setAdminid(adminId);
        admin.setAdminname(adminName);
        admin.setAdminpassword(adminPassword);
        admin = adminRepository.save(admin);
        if(admin!=null) {
            result.put("code", 200);
            result.put("admin", admin);
        }else{
            result.put("code", 400);
        }
        //System.out.print(stu.getStuid()+ stu.getStuname());
        return result;
    }

    /*
    * 用id删除管理员*/
    @DeleteMapping(value = "/admins/{adminId}")
    public  Map<String,Object> deleteAdmin(@PathVariable("adminId") String adminId){
        Admin admin = new Admin();
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",400);
        List<Admin> list = adminRepository.findAll();
        for(int i=0;i<list.size();i++){
            String aId = list.get(i).getAdminid();
            if(aId.equals(adminId)){
                admin.setAdminid(adminId);
                admin.setAdminname(list.get(i).getAdminname());
                admin.setAdminpassword(list.get(i).getAdminpassword());
                adminRepository.delete(admin);
                result.put("code",200);
                break;
            }
        }
        return result;
    }
}
